package cn.manyufun.myf.ui.main.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.CountDownTimer;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.OnClick;
import cn.manyufun.myf.R;
import cn.manyufun.myf.api.Api;
import cn.manyufun.myf.api.HostType;
import cn.manyufun.myf.bean.PosterBean;
import cn.manyufun.myf.common.base.BaseActivity;
import cn.manyufun.myf.common.baserx.RxSchedulers;
import cn.manyufun.myf.common.baserx.RxSubscriber;
import cn.manyufun.myf.common.commonutils.ImageLoaderUtils;
import cn.manyufun.myf.common.commonutils.LogUtils;

public class WelcomeActivity extends BaseActivity {
    private static final long ANIMATION_TIME = 4000;
    private static final float SCALE_END = 1.13F;
    @BindView(R.id.loadingImageView)
    View mLoadingView;
    @BindView(R.id.btn_ad_ignore)
    Button mBtnAdIgnore;
    @BindView(R.id.ad)
    ImageView mSplashImage;
    private Random mRandom;

    @OnClick(R.id.btn_ad_ignore)
    void submit() {
        LogUtils.logd("submittttt");
        if (myCountDownTimer != null)
            myCountDownTimer.cancel();
        goMain();
        finish();
    }

    private MyCountDownTimer myCountDownTimer;

    public void countDownStart() {
        long countDownInterval = 1000;// 间隔时间
        myCountDownTimer = new MyCountDownTimer(ANIMATION_TIME,
                countDownInterval);
        myCountDownTimer.start();
    }
    private void goMain() {
        MainActivity.startAction(WelcomeActivity.this);
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_welcome;
    }

    @Override
    public void initPresenter() {

    }

    @Override
    public void initView() {
        SetTranslanteBar();

        ObjectAnimator animatorX = ObjectAnimator.ofFloat(mSplashImage, "scaleX", 1f, SCALE_END);
//        ObjectAnimator animatorY = ObjectAnimator.ofFloat(mSplashImage, "scaleY", 1f, SCALE_END);

        AnimatorSet set = new AnimatorSet();
        set.setDuration(ANIMATION_TIME).play(animatorX);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
//                goMain();
//                finish();
            }
        });
        set.start();

        Api.getDefault(HostType.manyufun).getPosterList()
                .compose(RxSchedulers.<PosterBean>io_main())
        .subscribe(new RxSubscriber<PosterBean>(mContext, false) {
            @Override
            public void onStart() {
                super.onStart();
//                mView.showLoading(mContext.getString(R.string.loading));
            }

            @Override
            protected void _onNext(PosterBean posterBean) {
                List<String> urls = posterBean.getImageList();
                if(urls != null && urls.size()>0) {
                    mRandom = new Random();
                    String url = urls.get(mRandom.nextInt(urls.size()));
                    ImageLoaderUtils.simpleDisplay(mContext, mSplashImage, url);
                    mSplashImage.setVisibility(View.VISIBLE);
                }
                countDownStart();
            }

            @Override
            protected void _onError(String message) {
                LogUtils.loge(message);
                countDownStart();
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (myCountDownTimer != null) {
            myCountDownTimer.cancel();

        }
    }

    private class MyCountDownTimer extends CountDownTimer {

        public MyCountDownTimer(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        // 可直接更新UI
        @Override
        public void onTick(long millisUntilFinished) {
            mBtnAdIgnore.setText(millisUntilFinished / 1000 + " 跳过");// 转化为秒
        }

        @Override
        public void onFinish() {
            finish();
            goMain();
        }

    }
}
